package com.lanou3g.farmdoctor.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou3g.farmdoctor.R;
import com.lanou3g.farmdoctor.entity.QandABean;
import com.lanou3g.farmdoctor.stream.ReadStream;
import com.lanou3g.farmdoctor.volley.VolleySingleton;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by ChenFengYao on 15/11/24.
 * 问答页面的适配器
 */
public class QandAAdapter extends BaseAdapter {
    private Context context;
    private QandABean qandABean;
    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;
    public QandAAdapter(Context context) {
        this.context = context;
        initData();
    }

    private void initData() {
        //等待获得数据
       // qandABean = new QandABean();
        qandABean = ReadStream.getEntity(QandABean.class,R.raw.item_questions);
        volleySingleton = VolleySingleton.getInstance();
        imageLoader = volleySingleton._getImageLoader();
    }

    @Override
    public int getCount() {
        return qandABean==null?0:qandABean.getList().size();
    }

    @Override
    public QandABean.ListEntity getItem(int position) {
        return qandABean.getList().get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView==null){
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item_qanda,null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
       // QandABean qandABean = qandABeans.get(position);
        QandABean.ListEntity data = qandABean.getList().get(position);

        viewHolder.title.setText(data.getContent());
        viewHolder.body.setText(data.getExtra());
        viewHolder.place.setText(data.getDistrict());
        try {
        String url = data.getPicThumbs().get(0);
            ImageLoader.ImageListener listener = imageLoader.getImageListener(viewHolder.quesitonImg,
                    R.mipmap.ic_launcher,R.mipmap.ic_launcher);
           imageLoader.get(url,listener);

        }catch (Exception e){

            viewHolder.quesitonImg.setVisibility(View.GONE);
        }

        viewHolder.questionNum.setText(data.getReplycount());
        return convertView;
    }

    class ViewHolder{
        TextView title,body,place,questionNum;
        ImageView quesitonImg;

        public ViewHolder(View view) {
            title = (TextView) view.findViewById(R.id.tv_question_item_title);
            body = (TextView) view.findViewById(R.id.tv_question_item_place);
            place = (TextView) view.findViewById(R.id.tv_question_item_place);
            questionNum = (TextView) view.findViewById(R.id.tv_question_item_answer_num);
            quesitonImg = (ImageView) view.findViewById(R.id.iv_question_img);
        }
    }
}
